;**************************************************************
;	
;	This an simple implementation of  bootloader for S3C4510B
;	Hope it is useful to you.
;	
;	Duyunhai use this program to UART0
;	
;	Enjoy it! 
;
;**************************************************************


LOOPNUM		EQU		0x02
 
        AREA    Init, CODE, READONLY
		CODE32
		GET snds.s
		
        ENTRY

start



;Part 1
;***************************************************************
;disable interrupts in CPU and switch to SVC32 mode

	MRS	r0, cpsr
	BIC	r0, r0, #MASK_MODE
	ORR	r0, r0, #MODE_SVC32
	ORR 	r0, r0, #I_BIT
	ORR 	r0, r0, #F_BIT
	MSR	cpsr_c, r0

	LDR	r2, =ARM7_INTMASK			 ;R2->interrupt controller
	MVN	r1, #0						 ;&FFFFFFFF
	STR	r1, [r2]					 ;disable all interrupt soucres

	LDR	r2, =ARM7_INTPEND			 ;R2->interrupt pend register.
	MVN	r1, #0						 ;&FFFFFFFF
	STR	r1, [r2]					 ;clear all interrupt flags.


;Part 2
;****************************************************************

	LDR	r0, =ARM7_SYSCFG
	LDR	r1, =0x87ffffA0				;config SYSCFG
	STR	r1, [r0] 					;Cache & WB disabled


;Part 3
;***************************************************************
;	Import some important variables for later use

    IMPORT |Image$$RO$$Base|    
	IMPORT |Image$$RO$$Limit|   
	IMPORT |Image$$RW$$Base|   
	IMPORT |Image$$RW$$Limit| 	
	IMPORT |Image$$ZI$$Base| 	
	IMPORT |Image$$ZI$$Limit| 


;Part 4
;****************************************************************
;Initalize the memory as followa:
;	FLASH			@ 0  ~ 2 M
;	SDRAM			@ 2  ~ 18M


	LDR	r1, =rEXTDBWTH 				;EXTDBWTH          		
	LDR	r2, =rROMCON0  				;ROMCON0	@ 0M ~ 2M	
	LDR	r3, =rROMCON1				;ROMCON1	@ DISABLED
	LDR	r4, =rROMCON2				;ROMCON1	@ DISABLED
	LDR	r5, =rROMCON3				;ROMCON1	@ DISABLED
	LDR	r6, =rROMCON4				;ROMCON1	@ DISABLED
	LDR	r7, =rROMCON5				;ROMCON1	@ DISABLED
	LDR	r8, =rSDRAMCON0				;SDRAMCON0 	@ 2M ~ 18M
	LDR	r9, =rSDRAMCON1				;SDRAMCON1 	@ DISABLED
	LDR	r10,=rSDRAMCON2				;SDRAMCON2 	@ DISABLED
	LDR	r11,=rSDRAMCON3				;SDRAMCON3 	@ DISABLED
	LDR	r12,=rSREFEXTCON			
	
	LDR	r0, =ARM7_EXTDBWTH 			
	STMIA	r0, {r1-r12}


;Part 5
;***************************************************************
;Self copy from FLASH to SDRAM

	LDR		r0, =|Image$$RO$$Base|      
	LDR		r1, =|Image$$RO$$Limit|      
	LDR		r2, =|Image$$RW$$Base|      
	LDR		r3, =|Image$$RW$$Limit|     

	SUB 		r1, r1, r0
	SUB		r3, r3, r2
	ADD		r1, r1, r3
	
	LDR		r2, =0x200000   ;@2M

	
COPY
	LDR		r3, [r0], #4
	STR		r3, [r2], #4
	SUBS		r1, r1,   #4
	BNE		COPY


 
;Part 6
****************************************************************
;Remap the memory
; 	FLASH	@ 16 ~ 18M 
; 	SDRAM	@ 0  ~ 16M

  
	LDR	r1, =rEXTDBWTH_R 			;EXTDBWTH          		
	LDR	r2, =rROMCON0_R  			;ROMCON0	@ 16M ~ 18M	
	LDR	r3, =rROMCON1_R				;ROMCON1	@ DISABLED
	LDR	r4, =rROMCON2_R				;ROMCON2	@ DISABLED
	LDR	r5, =rROMCON3_R				;ROMCON3	@ DISABLED
	LDR	r6, =rROMCON4_R				;ROMCON4	@ DISABLED
	LDR	r7, =rROMCON5_R				;ROMCON4	@ DISABLED
	LDR	r8, =rSDRAMCON0_R			;SDRAMCON0 	@ 0M ~ 16M
	LDR	r9, =rSDRAMCON1_R			;SDRAMCON1 	@ DISABLED
	LDR	r10,=rSDRAMCON2_R			;SDRAMCON2 	@ DISABLED
	LDR	r11,=rSDRAMCON3_R			;SDRAMCON3 	@ DISABLED
	LDR	r12,=rSREFEXTCON_R			

	LDR	r0, =ARM7_EXTDBWTH 			
	STMIA	r0, {r1-r12}
  


;Part 7
;*****************************************************************
; Copy RW & ZI to SDRAM

	LDR r0, =|Image$$RO$$Limit| 	
	LDR r1, =|Image$$RW$$Base| 		
	LDR r3, =|Image$$ZI$$Base| 		

	CMP r0, r1 
	BEQ %1
 
0 	CMP r1, r3 						; Copy init data
	LDRCC r2, [r0], #4
	STRCC r2, [r1], #4
	BCC %0

1 	LDR r1, =|Image$$ZI$$Limit| 	; Top of zero init segment
	MOV r2, #0

2 	CMP r3, r1 						; Zero init
	STRCC r2, [r3], #4
	BCC %2

;Part 8
;*********************************************************************
;Use the UART0 to tranmit some byte


;;------------------------------------
;;	SerialComm program
;;------------------------------------			

;;	ARM7_ULCON0 (UART0s Line control Register) Initializatin			
;
;	[1:0] Word length per frame (WL)					
;		00 = 5 bits 01 = 6 bits
;		10 = 7 bits 11 = 8 bits						
;	[2] Number of Stop bits at the end of frame (STB)			
;		0 = One stop bit per frame
;		1 = Two stop bits per frame
;	[5:3] Parity mode (PMD)
;		0xx = No parity
;		100 = Odd parity
;		101 = Even parity
;		110 = Parity forced/ checked as 1.
;		111 = Parity forced/checked as 0.
;	[6] Serial clock selection (SC)
;		0 = Internal (MCLK)
;		1 = External (UCLK)
;	[7] Infra-red mode selection (IR)
;		0 = Normal mode operation
;;		1 = Infra-red Tx/Rx mode



	LDR	R1,=ARM7_ULCON0
	LDR	R0,=0x03	;8 bit ,one stop,no parity,MCLK,Normal mode
	STR	R0,[R1]


; 	ARM7_UCON0 (UART0s control Register) Initializatin			


	LDR	R1,=ARM7_UCON0
	LDR	R0,=0x09	;no receive interrupt,interrupt Tx mode,no DSR
	STR	R0,[R1]



;; 	ARM7_UBRDIV0 (UART0s baud rate divisor Register) 			
;
;	BRGOUT = (MCLK2 or UCLK)/(CNT0 + 1)/(16^CNT1)/16
;	
;	When MCLK=50MHz(MCLK2=MCLK/2)	
;			Baudrate=9600:	UBRDIV0=0xA20
;;			Baudrate=19200:	UBRDIV0=0x500


	LDR	R1,=ARM7_UBRDIV0
	LDR	R0,=0x500	;When MCLK=50MHz, Baudrate=19200
	STR	R0,[R1]

; 	Transmit the message from UART0 of S3C4510B to DCE 			


	LDR	R7,=LOOPNUM	

LOOP
	LDR	R1,=LINE1
	BL	PrintLine

	LDR	R1,=LINE2
	BL	PrintLine

	LDR	R1,=LINE3
	BL	PrintLine
	
	LDR	R1,=LINE4
	BL	PrintLine
	
	LDR	R1,=LINE5
	BL	PrintLine

	SUBS	R7,R7,#1
	BNE	LOOP

STOP
;        MOV     r0, #0x18       ; angel_SWIreason_ReportException
;        LDR     r1, =0x20026    ; ADP_Stopped_ApplicationExit
;        SWI     0x123456        ; Angel semihosting ARM SWI

	NOP
	B	STOP

;Subroute----PrintLine

PrintLine
	
	MOV	R4,LR
	MOV	R5,R1

Line
	LDRB	R2,[R5],#1
	AND		R1,R2,#&FF
	TST		R1,#&FF
	MOVEQ	PC,R4
	
	BL		PrintByte
	NOP
	MOV		R0,R0
	NOP
	B		Line



PrintByte
	LDR		R3,=ARM7_USTAT0	;Query the TBE bit,if bit=0,waiting...
	LDR		R6,[R3]
	TST		R6,#&40
	BEQ		PrintByte		;TBE=0

	LDR		R3,=ARM7_UTXBUF0	;TBE=1,and write data to UTXBUF0 and transmit 
	STR		R2,[R3]
	MOV		PC,LR
	


	AREA PRINTLINEOUT,DATA,READWRITE


LINE1	DCB		&A,&D,"***************************************",0
LINE2	DCB 	&A,&D,"  hello world,i am qrsgate2004 in ARM ",0
LINE3	DCB 	&A,&D,"  These is the Serial IO test program!",0
LINE4	DCB 	&A,&D,"          Let us go on!!",0
LINE5	DCB		&A,&D,"***************************************",&A,&D,&A,&D,0


        END

